//
//  ViewController.swift
//  19.1 scrollview
//
//  Created by 李煜 on 2024/6/26.
//

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {
    //a是scrollview的子视图
    var a: UIView = UIView()
    //b是a的子视图
    var b: UIView = UIView()
    
    var scroll: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        s1()
        testContentSize()
    }
    
    //https://www.jianshu.com/p/9091e5f34df5
    //验证contentSize
    func testContentSize() {
        let scroll = UIScrollView(frame: CGRectMake(0, 100, 200, 200))
        view.addSubview(scroll)
        scroll.delegate = self
        scroll.backgroundColor = .systemRed
        self.scroll = scroll
        
        let content = UIView(frame: CGRectMake(0, 0, 200, 400))
        scroll.addSubview(content)
        content.backgroundColor = .init(red: 117 / 255.0, green: 179 / 255.0, blue: 191 / 255.0, alpha: 0.4)
        
        let contentSubView = UIView(frame: CGRectMake(0, -50, 200, 50))
        contentSubView.backgroundColor = .black.withAlphaComponent(0.6)
        content.addSubview(contentSubView)

        //contentsize
        scroll.contentSize = CGSizeMake(200, 400)
//        scroll.contentOffset = CGPointMake(0, -50)
//        scroll.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
//        scroll.contentSize = content.bounds.size
    }
    func s1(){
        let scroll = UIScrollView(frame: CGRectMake(0, 100, 180, 180))
        view.addSubview(scroll)
        scroll.backgroundColor = .gray
        let img = UIImage(named: "cats")!
        let imgv = UIImageView(image: img)
        scroll.addSubview(imgv)
        print(img.size)
                scroll.contentSize = img.size
        //偏移量
        scroll.contentOffset = CGPointMake(-360, 0)
        scroll.delegate = self
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        scroll.setContentOffset(CGPoint(x: 0, y: -50), animated: true)
    }
    
}
extension ViewController {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
    }
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
//      called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
        print("手动设置setContentOffset动画停止")
    }
}

